<template>
  <div class="container">
    <el-card class="box-card">
      试题录入
      <!-- 表单 -->
      <el-form
        ref="form"
        :model="form"
        label-width="100px"
        size="small"
        :rules="rules"
      >
        <!-- 学科 -->
        <el-form-item label="学科:" prop="subjectID">
          <el-select
            v-model="form.subjectID"
            placeholder="请选择"
            style="width: 40%"
          >
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="(item, index) in subjectIDlist"
              :key="index"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 目录 -->
        <el-form-item label="目录:" prop="catalogID">
          <el-select
            v-model="form.catalogID"
            placeholder="请选择"
            style="width: 40%"
            :clearable="true"
          >
            <div v-if="this.form.subjectID">
              <el-option
                :label="item.directoryName"
                :value="item.id"
                v-for="item in catalogIDlist2"
                :key="item.id"
              >
              </el-option>
            </div>
          </el-select>
        </el-form-item>
        <!-- 企业 -->
        <el-form-item label="企业:" prop="enterpriseID">
          <el-select
            v-model="form.enterpriseID"
            placeholder="请选择"
            style="width: 40%"
          >
            <el-option
              :label="item.company"
              :value="item.id"
              v-for="item in enterpriseIDlist"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 城市 -->
        <el-form-item label="城市:" style="width: 40%" prop="city">
          <el-col :span="11">
            <!-- 市 -->
            <el-select v-model="form.province" placeholder="请选择">
              <el-option
                :label="item"
                :value="item"
                v-for="(item, index) in provincelist"
                :key="index"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="2"> </el-col>
          <el-col :span="11">
            <!-- 区 -->
            <el-select v-model="form.city" placeholder="请选择">
              <div v-if="this.form.province">
                <el-option
                  :label="item"
                  :value="item"
                  v-for="(item, index) in citylist"
                  :key="index"
                ></el-option>
              </div>
            </el-select>
          </el-col>
        </el-form-item>
        <!-- 方向 -->
        <el-form-item label="方向:" prop="direction">
          <el-select
            v-model="form.direction"
            placeholder="请选择"
            style="width: 40%"
          >
            <el-option
              :label="item"
              :value="item"
              v-for="(item, index) in directionlist"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 题型 -->
        <el-form-item label="题型:" prop="questionType">
          <el-radio-group v-model="form.questionType">
            <el-radio label="1" checked>单选</el-radio>
            <el-radio label="2">多选</el-radio>
            <el-radio label="3">简答</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 难度 -->
        <el-form-item label="难度:" prop="difficulty">
          <el-radio-group v-model="form.difficulty">
            <el-radio label="1">简单</el-radio>
            <el-radio label="2">一般</el-radio>
            <el-radio label="3">困难</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 题干 -->
        <!-- <el-form-item label="题干:" prop="question" >-->
          <!-- 富文本编辑器 -->
          <!-- <div id="demo1"></div>
        </el-form-item> -->
        <el-form-item label="题干:" prop="question">
           <quill-editor v-model="form.question" :options="editorOptions"></quill-editor>
        </el-form-item>
        <!-- 选项 -->
        <el-form-item label="选项:" v-if="form.questionType !== '3'">
          <!-- 单选 -->
          <template v-if="form.questionType === '1'">
            <div
              v-for="(item, index) in form.options"
              :key="index"
              class="xvanxiang"
            >
              <el-radio
                v-model="item.isRight"
                :label="Boolean('true')"
                >{{ item.code + ":" }}</el-radio
              >
              <el-input style="width:20%" v-model="item.title"></el-input>

              <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
              >
              <span>上传图片</span>
              </el-upload>
            </div>
            <el-button type="danger" disabled>+增加选项与答案</el-button>
          </template>
          <!-- 多选 -->
          <template v-if="form.questionType === '2'">
            <div
              v-for="(item, index) in form.options"
              :key="index"
              class="xvanxiang"
            >
              <el-checkbox v-model="item.isRight">{{
                item.code + ":"
              }}</el-checkbox>
              <el-input style="width:20%" v-model="item.title"></el-input>
              <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
              >
               <span>上传图片</span>
              </el-upload>
            </div>
            <el-button type="danger">+增加选项与答案</el-button>
          </template>
        </el-form-item>

        <!-- 解析视频 -->
        <el-form-item label="解析视频:" style="width: 40%">
          <el-input v-model="form.videoURL"></el-input>
        </el-form-item>
        <!-- 答案解析 -->
        <!-- <el-form-item label="答案解析:" prop="answer"  class="fix">
          <div id="demo2"></div>
        </el-form-item> -->
        <el-form-item   label="答案解析：" prop="answer">
           <quill-editor   v-model="form.answer" :options="editorOptions"></quill-editor>
        </el-form-item>
        <!-- 题目备注： -->
        <el-form-item label="题目备注:">
          <el-input
            type="textarea"
            v-model="form.remarks"
            style="width: 40%"
          ></el-input>
        </el-form-item>
        <!-- 试题标签 -->
        <el-form-item label="试题标签:">
          <el-select
            v-model="tags"
            placeholder="请选择"
            style="width: 40%"
            multiple
          >
            <el-option
              :label="item.tagName"
              :value="item.tagName"
              v-for="item in tagslist2"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="presentFn">{{
            this.form.id ? "确认编辑" : "确认提交"
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import E from 'wangeditor'
import { simple } from '@/api/hmmm/subjects'
import { contentList } from '@/api/hmmm/directorys' // 目录
import { firmlist } from '@/api/hmmm/companys'
import { provinces, citys } from '@/api/hmmm/citys'
import { direction as directiona } from '@/api/hmmm/constants'
// import { list as listAPI } from '@/api/hmmm/tags'
import { update, add, detail } from '@/api/hmmm/questions'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'

import hljs from 'highlight.js'
import 'highlight.js/styles/vs2015.css'

export default {
  name: 'QuestionNew',
  created () {
    // 学科列表
    this.simpleFn()
    // 目录
    this.listFn()
    // 标签
    // this.tagsdataFn()

    // 企业列表
    this.firmlistFn()
    //   试题修改
    this.compileAPI()
  },
  components: {
    quillEditor
  },
  data () {
    return {
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ],
          // 语法高亮
          syntax: {
            highlight: text => hljs.highlightAuto(text).value
          }
        }
      },
      id: Number(this.$route.query.id) || '', // id
      //   id: 36,
      question: '', // 富文本框暂存
      tags: [], // 标签暂存
      catalogID: '', // 目录暂存
      form: {
        subjectID: null, // 学科
        catalogID: '', // 目录
        enterpriseID: '', // 企业
        province: '', // 城市
        city: '', // 地区
        direction: '', // 方向
        questionType: '1', // 题型
        difficulty: '', // 难度
        question: '', // 题干
        options: [
          {
            img: '',
            isRight: false,
            title: '',
            code: 'a'
          },
          {
            img: '',
            isRight: false,
            title: '',
            code: 'b'
          },
          {
            img: '',
            isRight: false,
            title: '',
            code: 'c'
          },
          {
            img: '',
            isRight: false,
            title: '',
            code: 'd'
          }
        ], // 选项
        videoURL: ' ', // 解析视频
        answer: '', // 答案解析
        remarks: '', // 题目备注
        tags: '' // 试题标签
      },

      rules: {
        subjectID: [{ required: true, message: '请输入学科', trigger: 'blur' }],
        catalogID: [{ required: true, message: '请输入目录', trigger: 'blur' }],
        enterpriseID: [
          { required: true, message: '请输入企业', trigger: 'blur' }
        ],
        province: [{ required: true, message: '请输入城市', trigger: 'blur' }],
        city: [{ required: true, message: '请输入城市', trigger: 'blur' }],
        direction: [{ required: true, message: '请输入方向', trigger: 'blur' }],
        questionType: [
          { required: true, message: '请输入题型', trigger: 'blur' }
        ],
        difficulty: [
          { required: true, message: '请输入难度', trigger: 'blur' }
        ],
        question: [{ required: true, message: '请输入题干', trigger: 'blur' }],
        answer: [{ required: true, message: '请输入答案解析', trigger: 'blur' }]
      },
      // 学科列表
      subjectIDlist: [],
      //   目录列表
      catalogIDlist: [],
      catalogIDlist2: [],
      // 公司列表
      enterpriseIDlist: [],
      // 省份列表
      provincelist: provinces(),
      //   方向列表
      directionlist: directiona,
      //   标签列表
      tagslist: [],
      tagslist2: []
    }
  },

  methods: {
    //   跳转过来我拿到id,发送请求 编辑
    async compileAPI () {
      if (this.$route.query.id) {
        const res = await detail({ id: this.id })
        this.form = res.data
      }
    },

    // 选中学科
    subjectFn (val) {
      // console.log(val)
      // 清空目录
      this.form.catalogID = ''
      //   //   清空标签
      //   this.tags = []
      // 调用目录
      this.listFn()
      //   //   调用标签
      //   this.tagsdataFn()
      //   处理目录
      this.catalogIDlist2 = this.catalogIDlist.filter(item => {
        return item.subjectID === val
      })
      //   //   处理标签
      //   this.tagslist2 = this.tagslist.filter((item) => {
      //     return item.subjectID === val
      //   })
      //   console.log(this.tagslist2)
    },

    // 获取学科选项数据
    async simpleFn () {
      const res = await simple()
      // console.log(res)
      this.subjectIDlist = res.data
    },
    // 获取目录数据
    async listFn () {
      const res = await contentList()
      // console.log(res)
      this.catalogIDlist2 = res.data.items
    },

    // 获取公司列表
    async firmlistFn () {
      const res = await firmlist()

      this.enterpriseIDlist = res.data.items
    },

    // 兜底验证
    presentFn () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 校验通过
          //   判断 form中是否有 id属性
          if (this.form.id) {
            // 有值为编辑
            const res = await update(this.form)
            //   成功提示
            if (res.status === 200) {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
            }
            // 成功后退回去
            this.$router.back()
          } else {
            //   没有值为录入
            const res = await add(this.form)
            //   成功提示
            if (res.status === 200) {
              this.$message({
                message: '录入成功',
                type: 'success'
              })
              this.$router.push({ path: 'list' })
            }
          }
        }
      })
    }
  },
  computed: {
    //   计算城市
    citylist () {
      return citys(this.form.province)
    }
  }
}
</script>

<style lang="scss" scoped>

.container {
  margin: 10px;
  background-color: #fff;

}
::v-deep .avatar-uploader{
    margin-left: 20px;
    display: inline-block;
}
::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #424141;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    vertical-align: middle;
    overflow: hidden;
    width: 85px;
    height: 50px;
    line-height: 50px;

  }
 ::v-deep .ql-editor {
   min-height: 200px;
   }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
.xvanxiang {
    margin-bottom: 10px;
}

</style>
